// 配置页面JavaScript
window.addEventListener('DOMContentLoaded', () => {
    console.log('配置页面加载完成');
    
    const form = document.getElementById('mqtt-config');
    
    // 表单提交事件
    form.addEventListener('submit', (e) => {
        e.preventDefault();
        console.log('表单提交');
        
        // 收集表单数据
        const config = {
            id: Date.now().toString(),
            clientId: document.getElementById('client-id').value,
            url: document.getElementById('url').value,
            port: document.getElementById('port').value,
            username: document.getElementById('username').value,
            password: document.getElementById('password').value,
            keepalive: document.getElementById('keepalive').value,
            subscribeTopic: document.getElementById('subscribe-topic').value,
            publishTopic: document.getElementById('publish-topic').value
        };
        
        console.log('收集到的配置:', config);
        
        // 保存配置到本地存储
        saveConfig(config);
        
        // 返回首页
        window.location.href = 'index.html';
    });
});

// 保存配置到本地存储
function saveConfig(config) {
    console.log('保存配置到本地存储');
    
    // 从本地存储获取现有连接
    const savedConnections = JSON.parse(localStorage.getItem('mqttConnections')) || [];
    
    // 添加新配置
    savedConnections.push(config);
    
    // 保存回本地存储
    localStorage.setItem('mqttConnections', JSON.stringify(savedConnections));
}